package cc.fs.zhihuapp.ui.activity.demo;

import android.os.Bundle;
import android.os.Message;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import cc.fs.zhihuapp.R;
import cc.fs.zhihuapp.ui.activity.helper.BaseActivity;
import cc.fs.zhihuapp.ui.adapter.RefashAdapter;
import cc.fs.zhihuapp.utils.RecyclerViewFooter;

/**
 * Created by fostion on 1/4/16.
 */
public class RefashRecycleActivity extends BaseActivity {

    @InjectView(R.id.swipeRefreshLayout)
    SwipeRefreshLayout swipeRefreshLayout;
    @InjectView(R.id.recyclerView)
    RecyclerView recyclerView;
    RefashAdapter refashAdapter;

    RecyclerViewFooter recyclerViewFooter;
    List<String> list = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.activity_refash_recycle);

        ButterKnife.inject(this);

        refashAdapter = new RefashAdapter(list);
        recyclerView.setAdapter(refashAdapter);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        swipeRefreshLayout.setColorSchemeColors(R.color.theme_primary);
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                runDelay(new Runnable() {
                    @Override
                    public void run() {
                        loadData();
                        swipeRefreshLayout.setRefreshing(false);
                    }
                },4000);
            }
        });

        recyclerViewFooter = RecyclerViewFooter.create(recyclerView, new Runnable() {
            @Override
            public void run() {
                swipeRefreshLayout.setEnabled(false);

                runDelay(new Runnable() {
                    @Override
                    public void run() {
                        loadData();
                        swipeRefreshLayout.setEnabled(false);
                        swipeRefreshLayout.setRefreshing(false);
                        recyclerViewFooter.loadFinish();
                    }
                },4000);
            }
        });
    }

    private void loadData(){
        list.add("1");
        list.add("2");
        list.add("3");
        list.add("3");
        list.add("3");
        list.add("3");
        list.add("3");
        list.add("3");
        list.add("3");
        list.add("3");
        refashAdapter.notifyDataSetChanged();
    }

    @Override
    public void handler(Message msg) {

    }
}
